#ifndef WEBSOCKET_FRAME_H
#define WEBSOCKET_FRAME_H

#include <vector>
#include <cstdint>
#include <string>

namespace websocket {

enum class Opcode : uint8_t {
    CONTINUATION = 0x0,
    TEXT = 0x1,
    BINARY = 0x2,
    CLOSE = 0x8,
    PING = 0x9,
    PONG = 0xA
};

class WebSocketFrame {
public:
    WebSocketFrame();
    WebSocketFrame(Opcode opcode, const std::vector<uint8_t>& payload, bool fin = true);
    
    // 序列化帧数据
    std::vector<uint8_t> serialize() const;
    
    // 解析帧数据
    bool parse(const std::vector<uint8_t>& data);
    
    // Getter方法
    bool isFin() const { return fin_; }
    Opcode getOpcode() const { return opcode_; }
    const std::vector<uint8_t>& getPayload() const { return payload_; }
    std::string getPayloadAsString() const;
    
    // 创建控制帧的便捷方法
    static WebSocketFrame createTextFrame(const std::string& text);
    static WebSocketFrame createCloseFrame(uint16_t code = 1000, const std::string& reason = "");
    static WebSocketFrame createPingFrame(const std::vector<uint8_t>& data = {});
    static WebSocketFrame createPongFrame(const std::vector<uint8_t>& data = {});

private:
    bool fin_;
    bool rsv1_, rsv2_, rsv3_;
    Opcode opcode_;
    bool masked_;
    uint64_t payload_length_;
    std::vector<uint8_t> masking_key_;
    std::vector<uint8_t> payload_;
};

} // namespace websocket

#endif // WEBSOCKET_FRAME_H